package controllers;

import java.util.Date;
import java.util.List;

import javax.persistence.Lob;

import org.apache.commons.lang.StringUtils;

import play.*;
import play.data.validation.Valid;
import play.mvc.*;
import play.mvc.Router.ActionDefinition;
import service.CommoditiesService;
import models.Attachment;
import models.Commodity;
import models.User;

/**
 * 商品模块控制器
 * 
 * @author justin
 *
 */
public class Commodities extends Controller {
	
	//GET /commodities
	public static void index(){
		
		String searchKeyword = params.get("_search");
		
		List<Commodity> commodities = null;
		
		if(searchKeyword != null){
			//搜索
			commodities = Commodity.find("byNameLike", "%" + searchKeyword + "%").fetch();
		} else {
			commodities = Commodity.findAll();
		}
		String username = session.get(GlobalVariables.SECURE_USERNAME);
		User user = User.find("byUsername", username).first();
		CommoditiesService.setCommodityPriceByUser(commodities, user);
		
		render(commodities);
	}
	
	//GET /commodities/{id}
	public static void show(Long id){
		Commodity commodity = Commodity.findById(id);
		String username = session.get(GlobalVariables.SECURE_USERNAME);
		User user = User.find("byUsername", username).first();
		CommoditiesService.setCommodityPriceByUser(commodity, user);
		render(commodity);
	}
}
